using System;
using System.Text;
using System.Web;

namespace Framework.Library.Core.Utils.Web
{
    public class UrlBuilder
    {
        private string _path;
        private HttpValueCollection _queryString;
        private string _target;

        public UrlBuilder()
        {
        }

        //public UrlBuilder(string url)
        //{
        //}

        public UrlBuilder(string url)
        {
            if (url == null)
                throw new ArgumentNullException("url");

            int targetStartPosition = url.IndexOf('#');

            // question mark found
            if (targetStartPosition != -1)
            {
                string target = url.Substring(targetStartPosition + 1, url.Length - targetStartPosition - 1);

                _target = HttpUtility.UrlDecode(target);
            }

            int queryStartPosition = url.IndexOf('?');

            // question mark found
            if (queryStartPosition != -1)
            {
                // get querystring and url without querystring
                string queryString = url.Substring(queryStartPosition + 1, url.Length - queryStartPosition - 1);
                url = url.Substring(0, queryStartPosition);

                _queryString = new HttpValueCollection(queryString, true, Encoding.UTF8);
            }

            _path = HttpUtility.UrlDecode(url);
        }

        public HttpValueCollection QueryString
        {
            get
            {
                if (_queryString == null)
                    _queryString = new HttpValueCollection();

                return _queryString;
            }
        }

        public string Target
        {
            get { return _target; }
            set { _target = value; }
        }

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        public override string ToString()
        {
            return ToString(true);
        }

        public string ToString(bool urlEncode)
        {
            var sb = new StringBuilder();

            if (urlEncode)
                sb.Append(HttpUtility.UrlEncode(_path));
            else
                sb.Append(_path);

            string queryString = _queryString.ToString(urlEncode);

            if (queryString.Length > 0)
                sb.Append("?");

            sb.Append(queryString);

            if (_target != null)
            {
                sb.Append("#");

                if (urlEncode)
                    sb.Append(HttpUtility.UrlEncode(_target));
                else
                    sb.Append(_target);
            }

            return sb.ToString();
        }

        public static string ModifyQueryString(string url, string name, string value, bool addIfNotFound)
        {
            var urlHelper = new UrlBuilder(url);

            if (urlHelper.QueryString[name] != null || addIfNotFound)
                urlHelper.QueryString[name] = value;

            return urlHelper.ToString();
        }
    }
}